//package com.groupbuying.payproducer.orderpay.service.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.aliyun.openservices.ons.api.Message;
//import com.aliyun.openservices.ons.api.Producer;
//import com.groupbuying.payproducer.wxpay.WXPayMsgConfig;
//import com.groupbuying.payproducer.orderpay.controller.WeiXinPayController;
//import com.groupbuying.payproducer.orderpay.entity.OrderPayMain;
//import com.groupbuying.payproducer.orderpay.service.OrderPayService;
//import com.groupbuying.payproducer.orderpay.service.WeiXinPayService;
//import com.groupbuying.payproducer.orderpay.utils.OrderPayFinanceConstant;
//import com.groupbuying.payproducer.wxpay.WXPayUtil;
//import org.apache.commons.lang.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @Author gxy
// * @Description 微信支付service
// * @Date 2019/3/20 10:52
// **/
//
//@Service
//public class WeiXinPayServiceImpl implements WeiXinPayService {
//    @Autowired
//    private OrderPayService orderPayService;
//    public static final Logger LOGGER = LoggerFactory.getLogger(WeiXinPayController.class);
//
//    @Autowired
//    private Producer producer;
//
//    @Value("${PropertyKeyConst.PreferentialOrderPayTopic}")
//    private String topic;
//    @Value("${PropertyKeyConst.UpdatePreferentialOrderPayStateTag}")
//    private String updatePreferentialOrderPayStateTag;
//    @Value("${PropertyKeyConst.UpdateOrderPayMainStateTag}")
//    private String updateOrderPayMainStateTag;
//    @Value("${PropertyKeyConst.OrderSplitTag}")
//    private String orderSplitTag;
//
//    @Override
//    public String wechatPaymentCallback(HttpServletRequest request) {
//        StringBuilder sb = new StringBuilder();
//        String respString = "fail";
//        sb.append("微信支付回调开始 ");
//        LOGGER.info("=================notify=============");
//        try {
//            request.setCharacterEncoding("utf-8");
//
//            Map<String, String> resultMap = WXPayUtil.xmlParserCallback(request);
//            String requestParamsStr = JSON.toJSONString(resultMap, SerializerFeature.DisableCircularReferenceDetect);
//            LOGGER.info("==========通知内容原始=============" + requestParamsStr + "\r\n");
//            String responseStr = JSON.toJSONString(resultMap, SerializerFeature.DisableCircularReferenceDetect);
//            LOGGER.info("==========通知内容=============" + responseStr + "\r\n");
//            if (StringUtils.isNotBlank(responseStr)) {
//                boolean verifyResult = WXPayUtil.isSignatureValid(resultMap, WXPayMsgConfig.KEY);
//                LOGGER.info("=================verifyResult：=============" + verifyResult);
//                if (verifyResult) {
//                    String status = resultMap.get("trade_status");
//                    String out_trade_no = resultMap.get("out_trade_no");
//                    String waterNo = resultMap.get("trade_no");
//                    String orderType = resultMap.get("extra_common_param");
//                    String total_fee = resultMap.get("total_amount");//支付金额 ，单位元
//                    if (StringUtils.isNotBlank(status) && "TRADE_SUCCESS".equals(status)) {
//                        LOGGER.info("=================resultMap：=============" + resultMap.toString());
//                        System.out.println("订单类型=====" + orderType);
//                        //result_code ="0" 表示成功
//                        if (StringUtils.isEmpty(orderType)) {
//                            OrderPayMain payMainModel = orderPayService.getByPayMainNo(out_trade_no);
//                            if (payMainModel != null) {
//                                orderType = payMainModel.getOrdertype();
//                            }
//                        }
//                        LOGGER.info("订单类型=====" + orderType);
//                        //此处可以在添加相关处理业务，校验通知参数中的商户订单号out_trade_no和金额total_fee是否和商户业务系统的单号和金额是否一致，一致后方可更新数据库表中的记录。
//                        //校验支付单号和钱对不对得上
//                        LOGGER.info("=================total_fee：=============" + total_fee);
//
//                        boolean moneyTrue = orderPayService.checkIsMinePayAndMoneyRight(out_trade_no, orderType, Double.parseDouble(total_fee));
//                        LOGGER.info("=================checkIsMinePayAndMoneyRight：end=============");
//
//                        //处理成功业务
//                        String orderId = orderPayService.orderPaySuccess(out_trade_no, orderType, OrderPayFinanceConstant.PAY_TYPE_WEIXIN, waterNo);
//                        if (OrderPayFinanceConstant.ORDER_TYPE_PREFERENTIAL.equals(orderType)) {
//                            if (StringUtils.isNotBlank(orderId)) {
//                                try{
//                                    //发送消息至修改订单信息支付状态的mq
//                                    Message messageUpdateOrderState = new Message(topic,updatePreferentialOrderPayStateTag, JSON.toJSONString(new HashMap<>(), SerializerFeature.DisableCircularReferenceDetect).getBytes());
//                                    producer.send(messageUpdateOrderState);
//                                    //发送消息至修改订单支付单信息支付状态的mq
//                                    Message messageUpdatePayMainState = new Message(topic,updateOrderPayMainStateTag, JSON.toJSONString(new HashMap<>(), SerializerFeature.DisableCircularReferenceDetect).getBytes());
//                                    producer.send(messageUpdatePayMainState);
//                                    //发送消息至结算的mq
//                                    Message messageOrderSplit = new Message(topic,orderSplitTag, JSON.toJSONString(new HashMap<>(), SerializerFeature.DisableCircularReferenceDetect).getBytes());
//                                    producer.send(messageOrderSplit);
//                                }catch (Exception e){
//                                    e.printStackTrace();
//                                    LOGGER.error("微信支付回调发送至后续处理mq失败！" + e.getMessage());
//                                }
////                                pushOrder = preferentialordermainService.getByOrderId(orderId);
////                                if (PreferentialordermainModel.SPLITSTATE_WFZ.equals(pushOrder.getSplitState())
////                                        && PreferentialOrderParamModel.ORDER_STATE_SUCCESS.equals(pushOrder.getOrderState())) {
////                                    pushOrder.setSplitState(PreferentialordermainModel.SPLITSTATE_FZING);
////                                    preferentialordermainService.update(pushOrder);
////                                    preferentialOrderSettlement_P2Service.pushOrderToSplitMQ(orderId);
////                                }
//                            }
//                        }
//                        LOGGER.info("=================businessPaySuccess：end=============");
//
//
//                        respString = "success";
//                    } else {
//                        if (!OrderPayFinanceConstant.ORDER_TYPE_REDRECHARGE.equals(orderType)) {
////                            orderPayMain_P10Service.businessPayFail(out_trade_no, orderType, BaseSysOperModel.SYSTEMOPER);
//                        } else {
////                            storeRedEnvelopeRecharge_P3Service.failBusiness(out_trade_no);
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return respString;
//    }
//}
